<?php

namespace app\common\lib;

use app\common\upyun\Config;
use app\common\upyun\Signature;
use app\common\upyun\Util;
/**
 * 文件上传处理
 */
class Uploader
{
    const BUCKET_NAME = 'hxtz-8518';
    const OPERATOR_NAME = 'wealth';
    const OPERATOR_PWD = 'wealth8518';
    const HOST = 'http://v0.api.upyun.com';
    const FORMAPIKEY = '0gd8xZn5fsqfMj/iNqxI4VzfGTQ=';


    public function uploadFromApi($save_path){
        $config = new Config(self::BUCKET_NAME, self::OPERATOR_NAME, self::OPERATOR_PWD);
        $config->setFormApiKey(self::FORMAPIKEY);

        $data['save-key'] = $save_path;
        $data['expiration'] = time() + 120;
        $data['bucket'] = self::BUCKET_NAME;
        $policy = Util::base64Json($data);
        $method = 'POST';
        $uri = '/' . $data['bucket'];
        $signature = Signature::getBodySignature($config, $method, $uri, null, $policy);
        return json_encode(array(
            'policy' => $policy,
            'authorization' => $signature
        ));
    }
}